//
//  SDPaientListViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/7/9.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPaientListViewController.h"
#import "SDPaientEditorViewController.h"
#import "SDNavigationController.h"
#import "SDDelPaientInfoData.h"
#import "SDPaientInfoMgr.h"

@interface SDPaientListViewController()<SDFormBaseDataDelegate>
@property(nonatomic,strong)SDDelPaientInfoData* delPaientData;
@end

@implementation SDPaientListViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubviews];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (BOOL)navigationBarHidden
{
    return NO;
}

-(void)initSubviews
{
    [super initSubviews];
    BOOL naviHidden = [self navigationBarHidden];
    if (!naviHidden && self.navBar != nil)
    {
        [self.navBar setTitle:@"患者列表"];
        UIImage* addImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"actionbar_btn_add.png");
        [self.navBar customNaviLeftButton:self action:@selector(onCreateNewPaient:) image:addImg];
        [self.navBar customNaviRightButton:self action:@selector(onDelete:) title:@"编辑"];
    }
}

-(void)initData
{
    [super initData];
    
    _delPaientData = [[SDDelPaientInfoData alloc] init];
    _delPaientData.delegate = self;
}

#pragma mark - ui event
-(void)onCreateNewPaient:(id)sender
{
    SDLoginManager* loginMgr = [SDLoginManager sharedInstance];
    if (![loginMgr isLogin]) {
        [self showLoginViewController];
        return;
    }
    
    SDPaientEditorViewController* editorViewController = [[SDPaientEditorViewController alloc] init];
    
    SDNavigationController* navigationCtrl = [[SDNavigationController alloc] initWithRootViewController:editorViewController];
    [self presentViewController:navigationCtrl animated:YES completion:nil];
}

-(void)onDelete:(id)sender
{
    if (CHECK_VALID_ARRAY(self.dataSource)) {
        [self.friendTableView setEditing:!self.friendTableView.isEditing animated:YES];
        NSString* title = @"编辑";
        if (self.friendTableView.editing) {
            title = @"完成";
        }
        [self.navBar.editBtn setTitle:title forState:UIControlStateNormal];
    }
  }

#pragma mark - Table View Delegate
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}


-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return @"删除";
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    dispatch_async( dispatch_get_main_queue(), ^{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"你确定要删除吗？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSArray *value = [self.allDataSource objectForKey:self.indexDataSource[indexPath.section]];
            SDPaientInfoItem* userItem = value[indexPath.row];
            bool isSuccess = [[SDPaientInfoMgr shareAGDatabaseManager] deleteTableName:kPaientTableName where:[NSString stringWithFormat:@"pId = %ld",(long)userItem.pId]];
            if (isSuccess) {
                
//                _delPaientData.pId = userItem.pId;
//                [_delPaientData excuteHttpRequest];
                [self reloadData];
            }
        }];
        [alertController addAction:confirmAction];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    } );

}

#pragma mark - SDFormBaseDataDelegate
-(void) willUploadOfFormData:(SDFormBaseData*) formdata
{
    
}

-(void) formData:(SDFormBaseData*)formdata userInfo:(NSDictionary*) userInfo didFinishLoadWithError:(NSError*)error
{
    if (!error) {
        if ([formdata isKindOfClass:[SDDelPaientInfoData class]]) {

        }
    } else {
        
    }

}
@end
